<template>
  <view class="address-page">
    <!-- 地址列表 -->
    <view class="address-list" v-if="addressList.length">
      <view
        class="address-card"
        v-for="(item, index) in addressList"
        :key="index"
        @tap="selectAddress(item)"
      >
        <view class="card-left">
          <view class="user-info">
            <text class="name">{{ item.name }}</text>
            <text class="phone">{{ item.tel }}</text>
            <text class="tag" v-if="item.isDefault">默认</text>
          </view>
          <view class="address-detail">
            {{ item.addressDetail }}{{ item.houseNumber ? ' ' + item.houseNumber : '' }}
          </view>
        </view>
        <view class="card-right">
          <view class="action-icon" @tap.stop="editAddress(item)">
            <u-icon name="edit-pen" size="20" color="#666"></u-icon>
          </view>
          <view class="action-icon" @tap.stop="deleteAddress(item)">
            <u-icon name="trash" size="20" color="#666"></u-icon>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-else>
      <image src="/static/images/about/empty-address.png" mode="aspectFit"></image>
      <text>还没有收货地址</text>
      <text class="tip">快去添加一个吧</text>
    </view>

    <!-- 底部按钮 -->
    <view class="footer-bar">
      <button class="add-btn" @tap="addAddress">
        <u-icon name="plus" size="20" color="#fff"></u-icon>
        <text>新增收货地址</text>
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      addressList: [],
      source: '' // 来源页面标识
    }
  },
  onLoad(options) {
    // 保存来源页面标识
    if (options.source) {
      this.source = options.source
    }
  },
  onShow() {
    this.getAddressList()
  },
  methods: {
    // 获取地址列表
    async getAddressList() {
      try {
        const { errno, errmsg, data } = await this.$get('address/list')
        if (errno === 0) {
          this.addressList = data.list
        } else {
          uni.showToast({
            title: errmsg,
            icon: 'none'
          })
        }
      } catch (error) {
        console.log(error)
        uni.showToast({
          title: '获取地址列表失败',
          icon: 'none'
        })
      }
    },
    // 选择地址（从订单页进入时使用）
    selectAddress(address) {
      if (this.source === 'checkout') {
        // 构造完整地址对象
        const fullAddress = {
          ...address,
          address: address.province + address.city + address.county + address.addressDetail
        }
        // 返回上一页并传递选中的地址
        const pages = getCurrentPages()
        const prevPage = pages[pages.length - 2]
        if (prevPage) {
          prevPage.$vm.updateAddress(fullAddress)
          uni.navigateBack()
        }
      }
    },
    // 新增地址
    addAddress() {
      uni.navigateTo({
        url: '/pages/addresses/add'
      })
    },
    // 编辑地址
    editAddress(item) {
      uni.navigateTo({
        url: `/pages/addresses/add?id=${item.id}`
      })
    },
    // 删除地址
    async deleteAddress(item) {
      try {
        const [error, res] = await uni.showModal({
          title: '提示',
          content: '确认删除该地址吗？',
          confirmText: '删除',
          confirmColor: '#fa3534'
        })
        
        if (error || !res.confirm) return
        
        const { errno, errmsg } = await this.$post('address/delete', { id: item.id })
        if (errno === 0) {
          uni.showToast({
            title: '删除成功',
            icon: 'success'
          })
          this.getAddressList()
        } else {
          uni.showToast({
            title: errmsg,
            icon: 'none'
          })
        }
      } catch (error) {
        console.log(error)
        uni.showToast({
          title: '删除失败',
          icon: 'none'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/common/theme.scss';

.address-page {
  min-height: 100vh;
  background-color: $bg-main;
  padding: 20rpx 24rpx;
  padding-bottom: calc(160rpx + env(safe-area-inset-bottom));
  box-sizing: border-box;
}

.address-list {
  .address-card {
    display: flex;
    align-items: center;
    background-color: $bg-white;
    padding: 30rpx;
    border-radius: 16rpx;
    margin-bottom: 24rpx;
    box-shadow: 0 8rpx 16rpx rgba(0, 0, 0, 0.02);

    .card-left {
      flex: 1;
      .user-info {
        display: flex;
        align-items: baseline;
        margin-bottom: 16rpx;

        .name {
          font-size: 32rpx;
          font-weight: 600;
          color: $primary-dark-pink;
          margin-right: 16rpx;
        }

        .phone {
          font-size: 28rpx;
          color: $primary-light-pink;
          margin-right: 16rpx;
        }

        .tag {
          font-size: 22rpx;
          color: $primary-pink;
          background-color: $bg-notification;
          padding: 4rpx 12rpx;
          border-radius: 6rpx;
          font-weight: 500;
        }
      }

      .address-detail {
        font-size: 28rpx;
        color: $primary-light-pink;
        line-height: 1.5;
      }
    }

    .card-right {
      display: flex;
      align-items: center;
      margin-left: 20rpx;
      padding-left: 20rpx;
      border-left: 2rpx solid $bg-main;
      .action-icon {
        width: 60rpx;
        height: 60rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;

        & + .action-icon {
          margin-left: 10rpx;
        }
      }
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 25vh;

  image {
    width: 280rpx;
    height: 280rpx;
    margin-bottom: 30rpx;
  }

  text {
    font-size: 30rpx;
    color: $primary-light-pink;
  }
  .tip {
    font-size: 26rpx;
    color: #cccccc;
    margin-top: 12rpx;
  }
}

.footer-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: $bg-white;
  padding: 20rpx 30rpx;
  padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.03);

  .add-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 88rpx;
    background: $gradient-button;
    color: $bg-white;
    font-size: 30rpx;
    font-weight: 500;

    text {
      margin-left: 12rpx;
    }
  }
}
</style>
